package net.lab1024.sa.admin.module.business.file.service;

import java.util.List;
import net.lab1024.sa.admin.module.business.file.dao.DocmentabDao;
import net.lab1024.sa.admin.module.business.file.domain.entity.DocmentabEntity;
import net.lab1024.sa.admin.module.business.file.domain.form.add.DocmentabAddForm;
import net.lab1024.sa.admin.module.business.file.domain.form.query.DocmentabQueryForm;
import net.lab1024.sa.admin.module.business.file.domain.form.update.DocmentabUpdateForm;
import net.lab1024.sa.admin.module.business.file.domain.vo.DocmentabVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 生产订单与机器对应表 Service
 *
 * @Author AKITA
 * @Date 2025-07-09 17:14:21
 * @Copyright AKITA
 */

@Service
public class DocmentabService {

    @Resource
    private DocmentabDao docmentabDao;

    /**
     * 分页查询
     */
    public PageResult<DocmentabVO> queryPage(DocmentabQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<DocmentabVO> list = docmentabDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(DocmentabAddForm addForm) {
        DocmentabEntity docmentabEntity = SmartBeanUtil.copy(addForm, DocmentabEntity.class);
        Integer lineNum = docmentabDao.selectMaxLineNum(addForm.getDocEntry());
        docmentabEntity.setLineNum(++lineNum);
        docmentabDao.insert(docmentabEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(DocmentabUpdateForm updateForm) {
        DocmentabEntity docmentabEntity = SmartBeanUtil.copy(updateForm, DocmentabEntity.class);
        docmentabDao.updateById(docmentabEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        docmentabDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long docEntry) {
        if (null == docEntry){
            return ResponseDTO.ok();
        }

        docmentabDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }
}
